/*
 * Basic application user account
 */
package com.spartancoder.accommodation.models;

import com.spartancoder.accommodation.beans.AdministratorBean;
import com.spartancoder.accommodation.db.Database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

/**
 *
 * @author andrei
 */
public class Administrator {

	private static Logger log = Logger.getLogger(Administrator.class.getName());
	private static Database db = Database.getInstance();

	public static void create(AdministratorBean ab, String password) {
		try {
			Connection conn = db.getConnection();
			String query = "insert into `administrators`(`user`, `password`)" +
					"values(?, md5(?))";

			PreparedStatement st = conn.prepareStatement(query);
			st.setString(1, ab.getUser());
			st.setString(2, password);

			st.execute();

			st.close();
			conn.close();

		} catch (SQLException ex) {
			log.severe(ex.toString());
			throw new InternalError(ex.toString());
		}
	}

	public static void update(AdministratorBean ab) {
		try {
			Connection conn = db.getConnection();
			String query = "update `administrators` set `user`=? " +
					"where `administrator_id`=?";

			PreparedStatement st = conn.prepareStatement(query);
			st.setString(1, ab.getUser());
			st.setInt(2, ab.getAdministratorId());

			st.executeUpdate();

			st.close();
			conn.close();

		} catch (SQLException ex) {
			log.severe(ex.toString());
			throw new InternalError(ex.toString());
		}
	}

	public static void updatePassword(int administratorId, String password) {
		try {
			Connection conn = db.getConnection();
			String query = "update `administrators` set `password`=md5(?) " +
					"where `administrator_id`=?";

			PreparedStatement st = conn.prepareStatement(query);
			st.setString(1, password);
			st.setInt(2, administratorId);

			st.executeUpdate();

			st.close();
			conn.close();

		} catch (SQLException ex) {
			log.severe(ex.toString());
			throw new InternalError(ex.toString());
		}
	}

	public static boolean exists( String user ) {
		try {
			Connection conn = db.getConnection();
			String query = "select count(*) from `administrators` where `user`=?";
			PreparedStatement st = conn.prepareStatement(query);
			st.setString(1, user);

			ResultSet rs = st.executeQuery();
			if( rs.next() ) {
				if( rs.getInt(1) == 1 ) {
					return true;
				}
			}

			return false;
		} catch( SQLException ex ) {
			log.severe(ex.toString());
			throw new InternalError(ex.toString());
		}
	}

	public static void remove( int adminId ) {
		try {
			Connection conn = db.getConnection();
			String query = "delete from `administrators` where `administrator_id`=?";
			
			PreparedStatement st = conn.prepareStatement(query);
			st.setInt(1, adminId);
			st.execute();
		} catch( SQLException ex ) {
			log.severe(ex.toString());
			throw new InternalError(ex.toString());
		}
	}

	public static AdministratorBean fetch(String user, String password) {
		try {
			Connection conn = db.getConnection();
			String query = "select * from `administrators` " +
					"where `user`=? and password=md5(?)";

			PreparedStatement st = conn.prepareStatement(query);
			st.setString(1, user);
			st.setString(2, password);

			ResultSet rs = st.executeQuery();
			if (rs.next()) {
				AdministratorBean ab = new AdministratorBean();
				ab.fill(rs);

				rs.close();
				st.close();
				conn.close();

				return ab;
			}

			st.close();
			conn.close();

			return null;
		} catch (SQLException ex) {
			log.severe(ex.toString());
			throw new InternalError(ex.toString());
		}
	}

	public static AdministratorBean fetch( int administratorId ) {
		try {
			Connection conn = db.getConnection();
			String query = "select * from `administrators` " +
					"where `administrator_id`=?";

			PreparedStatement st = conn.prepareStatement(query);
			st.setInt(1, administratorId);

			ResultSet rs = st.executeQuery();
			if (rs.next()) {
				AdministratorBean ab = new AdministratorBean();
				ab.fill(rs);

				rs.close();
				st.close();
				conn.close();

				return ab;
			}

			st.close();
			conn.close();

			return null;
		} catch (SQLException ex) {
			log.severe(ex.toString());
			throw new InternalError(ex.toString());
		}
	}

	public static List<AdministratorBean> fetchAll() {
		try {
			Connection conn = db.getConnection();
			String query = "select * from `administrators`";
			PreparedStatement st = conn.prepareStatement(query);

			ResultSet rs = st.executeQuery();
			List<AdministratorBean> ret = new LinkedList<AdministratorBean>();
			while(rs.next()) {
				AdministratorBean ab = new AdministratorBean();
				ab.fill(rs);
				ret.add(ab);
			}

			rs.close();
			st.close();
			conn.close();

			return ret;
		} catch( SQLException ex) {
			log.severe(ex.toString());
			throw new InternalError(ex.toString());
		}
	}
}
